<script>
    function submitOnEnter(commandId, e)
    {
        var keycode;
        if (window.event) {
            keycode = window.event.keyCode;
        } else if (e) {
            keycode = e.which;
        } else  {
            return true;
        }
              
        if (keycode == 13) {
            document.getElementById(commandId).click();
            return false;
        } else {
            return true;
        }
    }
</script>

<%-- 
    Document   : nicks
    Created on : 19-oct-2008, 17:28:29
    Author     : org.@le

--%>

<%@taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@taglib uri="http://richfaces.org/rich" prefix="rich"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <c:if test="${sessionBean.ejbSession.owner==null}" >
            <jsp:forward page="../login.jsp" />
        </c:if>
        <c:if test="${!sessionBean.ejbSession.owner.adminUser}" >
            <jsp:forward page="../user/homeUser.jsp" />
        </c:if>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <f:view>
            <a4j:region>
            <h:form>
            <a4j:commandButton style="right:10px; top:10px; position:absolute;" value="Clean users" action="#{UsersBean.checkingUsers}" reRender="form1, formUsers"/>
            <h:panelGrid width="100%">
                <rich:toolBar style="position:absolute; top: 10px; left: 10px" width="300px">
                    <a href="/site-es/faces/admin/admin.jsp" >Admin panel</a> > 
                    <h:outputText value="Manage users" />
                </rich:toolBar>
            </h:panelGrid>
            <br/>
            <h2>Manage users</h2>
                <a4j:commandButton id="add" image="/images/add2.png" reRender="form1">
                    <f:setPropertyActionListener target="#{UsersBean.newUser}" value="#{true}" />
                    <f:setPropertyActionListener target="#{UsersBean.linkRPOk}" value="#{false}" />
                    <f:setPropertyActionListener target="#{UsersBean.user}" value="#{null}" />
                    <rich:toolTip direction="top-right" >
                        Add new user
                    </rich:toolTip>
                </a4j:commandButton>
                
                <a4j:status>
                        <f:facet name="start">
                            <h:graphicImage  value="/images/ajax_process.gif"/>
                        </f:facet>
                    </a4j:status>
            </h:form>
                <br/>
            <h:form id="form1">
                <h:messages id="mal1" globalOnly="true"/>
                <rich:panel rendered="#{UsersBean.newUser}" >
                    <div align="right" >
                        <a4j:commandButton id="save" value="Guardar" action="#{UsersBean.saveUser}" type="submit" reRender="form1, formUsers"/>
                        <a4j:commandButton value="Cancel" immediate="true" reRender="form1" >
                            <f:setPropertyActionListener target="#{UsersBean.user}" value="#{null}" />
                            <f:setPropertyActionListener target="#{UsersBean.newUser}" value="#{false}" />
                            <f:setPropertyActionListener target="#{UsersBean.linkRPOk}" value="#{false}" />
                        </a4j:commandButton>
                    </div>
                    <h:panelGrid columns="3" >
                        <h:outputText value="(*) Email: " />
                        <h:inputText id="email" requiredMessage="Este campo es requerido." required="true" maxlength="50" value="#{UsersBean.user.email}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:message for="email" style="color: red" styleClass="errorMessage" />
                        
                        <h:outputText value="(*) Password: " />
                        <h:inputSecret id="pwd1" maxlength="15" value="#{UsersBean.pass1}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:message for="pwd1" style="color: red" styleClass="errorMessage" />
                        
                        <h:outputText value="(*) Re-type Password: " />
                        <h:inputSecret id="pwd2" maxlength="15" value="#{UsersBean.pass2}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:message for="pwd2" style="color: red" styleClass="errorMessage" />
                        
                        <h:outputText value="Status: " />
                        <h:selectOneMenu value="#{UsersBean.user.status}" >
                            <f:selectItem itemValue="active" itemLabel="Active" />
                            <f:selectItem itemValue="pending" itemLabel="Pending" />
                            <f:selectItem itemValue="reactive" itemLabel="Reactive" />
                        </h:selectOneMenu>
                        <h:outputText value="" />
                        <h:outputText value="Administrator: " />
                        <h:selectBooleanCheckbox value="#{UsersBean.user.adminUser}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:outputText value="" />
                        
                        <h:outputLink value="/site-es/faces/recoveryPassword.jsp" rendered="#{UsersBean.linkRPOk}" target="_blank">
                            <h:outputText value="Password Recovery" />
                        </h:outputLink>
                    </h:panelGrid>
                </rich:panel>
                </h:form>
                    
                <h:form id="formUsers">
                <rich:dataTable id="usersTable" columns="6" rows="100" value="#{UsersBean.users}" var="user" border="1" >
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Users" />
                        </f:facet>
                        <h:panelGrid id="column1" >
                            <h:outputText value="#{user.email}" />
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Status" />
                        </f:facet>
                        <h:panelGrid >
                            <h:outputText value="#{user.status}" />
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Administrator" />
                        </f:facet>
                        <h:panelGrid >
                            <h:outputText rendered="false" value="#{user.adminUser}" />
                            <h:outputText rendered="#{user.adminUser==true}" value="yes" />
                            <h:outputText rendered="#{user.adminUser==false}" value="no" />
                            
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                        </f:facet>
                        <h:panelGrid rendered="#{user.id != sessionBean.ejbSession.owner.id}" >
                            <a4j:commandButton id="add1" image="/images/edit.png" reRender="form1" onclick="javascript:scroll(0,0)">
                                <f:setPropertyActionListener target="#{UsersBean.newUser}" value="#{true}" />
                                <f:setPropertyActionListener target="#{UsersBean.linkRPOk}" value="#{false}" />
                                <f:setPropertyActionListener target="#{UsersBean.user}" value="#{user}" />
                                <rich:toolTip for="add1" direction="bottom-right" >
                                    Edit
                                </rich:toolTip>
                            </a4j:commandButton>
                            
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                        </f:facet>
                        <h:panelGrid rendered="#{user.id != sessionBean.ejbSession.owner.id}" >
                            <a4j:commandButton id="add2" image="/images/trash.png" action="#{UsersBean.removeUser}" reRender="form1, formUsers" onclick="if(!confirm('The User will be deleted. Are you sure?'))return false" >
                                <f:setPropertyActionListener target="#{UsersBean.user}" value="#{user}" />
                                <rich:toolTip direction="bottom-right" >
                                    Delete
                                </rich:toolTip>
                            </a4j:commandButton>
                        </h:panelGrid>
                    </rich:column>
                    
                </rich:dataTable>
                <rich:datascroller for="usersTable" maxPages="10" page="#{UsersBean.indexUsers}" />
            </h:form>
            </a4j:region>
        </f:view>
    </body>
</html>
